#include <stdio.h>
#include <stdlib.h>

typedef char DataType;

typedef struct node {
    DataType data;
    struct node *next;
} Stack;

Stack *init() {

    Stack *head = (Stack *) malloc(sizeof(Stack));
    head->data = '0';
    head->next = NULL;

    return head;
}

Stack *push(Stack *stack, DataType data) {

    Stack *new = (Stack *) malloc(sizeof(Stack));
    new->data = data;
    new->next = stack;

    stack = new;
    return new;
}

int isEmpty(Stack *stack) {
    return stack == NULL;
}


Stack *pop(Stack *stack) {

    if (isEmpty(stack)) {
        printf("stack is null");
        return NULL;
    }
    Stack *node = stack;
    printf("stack pop value=%c\n", node->data);

    Stack *next = stack->next;
    free(node);

    return next;
}

/**
 * 圆括号检测 (((()))
 * @return
 */
int expr() {

    Stack *top = NULL;
    DataType ch;

    while ((ch = getchar()) != '\n') {

        if (ch == '(') {
            top = push(top, ch);
        } else {
            if (ch == ')') {
                if (isEmpty(top)) {
                    return 0;
                } else {
                    top = pop(top);
                }
            }
        }
    }

    if (isEmpty(top)) {
        return 1;
    }
    return 0;
}


int main() {
    printf("Hello, World!\n");

    Stack *item = NULL;

    item = push(item, '0');
    item = push(item, '1');
    item = push(item, '2');
    item = push(item, '3');

    printf("current item top value= %c\n", item->data);

    item = pop(item);
    item = pop(item);
    item = pop(item);
    item = pop(item);
    item = pop(item);

    printf("please input some ((()))\n");
    int result = expr();
    printf("result is =%d", result);

    return 0;
}
